package com.lykj.modules.shop.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lykj.core.excel.util.ExcelUtil;
import com.lykj.core.secure.utils.AuthUtil;
import com.lykj.core.tool.utils.BeanUtil;
import com.lykj.core.tool.utils.DateUtil;
import com.lykj.modules.desk.vo.NoticeVO;
import com.lykj.modules.shop.entity.UserOrder;
import com.lykj.modules.shop.excel.UserBillExcel;
import com.lykj.modules.shop.excel.UserOrderExcel;
import com.lykj.modules.shop.vo.UserOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.lykj.core.mp.support.Condition;
import com.lykj.core.mp.support.Query;
import com.lykj.core.tool.api.R;
import com.lykj.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lykj.modules.shop.entity.UserBill;
import com.lykj.modules.shop.vo.UserBillVO;
import com.lykj.modules.shop.wrapper.UserBillWrapper;
import com.lykj.modules.shop.service.UserBillService;
import com.lykj.core.boot.ctrl.LykjController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 用户账单表 控制器
 *
 * @author cxl
 * @since 2022-01-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("lykj-shop/user/bill")
@Api(value = "用户账单表", tags = "用户账单表接口")
public class UserBillController extends LykjController {

	private final UserBillService userBillService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入userBill")
	public R<UserBillVO> detail(UserBill userBill) {
		UserBill detail = userBillService.getOne(Condition.getQueryWrapper(userBill));
		return R.data(UserBillWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 用户账单表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入userBill")
	public R<IPage<UserBillVO>> list(@ApiIgnore @RequestParam Map<String, Object> map, Query query) {//list(UserBillVO userBill, Query query) {
		if (query.getAscs() == null && query.getDescs() == null) {
			query.setDescs("id");
		}
		UserBillVO userBill = JSON.parseObject(JSON.toJSONString(map), UserBillVO.class);
//		IPage<UserBill> pages = userBillService.page(Condition.getPage(query), Condition.getQueryWrapper(userBill));
		IPage<UserBillVO> pages = userBillService.selectUserBillPage(Condition.getPage(query), userBill);
		return R.data(pages);
	}


	/**
	 * 自定义分页 用户账单表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入userBill")
	public R<IPage<UserBillVO>> page(UserBillVO userBill, Query query) {
		userBill.setUserId(AuthUtil.getUserId());
		if (query.getAscs() == null && query.getDescs() == null) {
			query.setDescs("id");
		}
		IPage<UserBillVO> pages = userBillService.selectUserBillPage(Condition.getPage(query), userBill);
		return R.data(pages);
	}

	/**
	 * 新增 用户账单表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入userBill")
	public R save(@Valid @RequestBody UserBill userBill) {
		return R.status(userBillService.save(userBill));
	}

	/**
	 * 修改 用户账单表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入userBill")
	public R update(@Valid @RequestBody UserBill userBill) {
		return R.status(userBillService.updateById(userBill));
	}

	/**
	 * 新增或修改 用户账单表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入userBill")
	public R submit(@Valid @RequestBody UserBill userBill) {
		return R.status(userBillService.saveOrUpdate(userBill));
	}


	/**
	 * 删除 用户账单表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(userBillService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导出账单
	 */
	@GetMapping("export")
	@ApiOperationSupport(order = 15)
	@ApiOperation(value = "导出账单", notes = "传入userBill")
	public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		UserBillVO userBill = JSON.parseObject(JSON.toJSONString(params), UserBillVO.class);
		if(Func.isNotEmpty(params.get("queryDate")) && !"undefined".equals(params.get("queryDate"))) {
			String[] date = params.get("queryDate").toString().split(",");
			userBill.setStartDate(date[0]);
			userBill.setEndDate(date[1]);
		}
//		LambdaQueryWrapper<UserBill> lqw = Wrappers.<UserBill>query().lambda();
//		if (Func.isNotEmpty(params.get("queryDate")) && !"undefined".equals(params.get("queryDate"))) {
//			String[] date = params.get("queryDate").toString().split(",");
//			lqw.ge(UserBill::getCreateTime, DateUtil.parse(date[0], DateUtil.PATTERN_DATE));
//			lqw.le(UserBill::getCreateTime, DateUtil.parse(date[1], DateUtil.PATTERN_DATE));
//		}
//		List<UserBill> list = userBillService.list(lqw);
//		List<UserBillExcel> listExcel = Objects.requireNonNull(BeanUtil.copy(list, UserBillExcel.class));
		List<UserBillExcel> listExcel = userBillService.export(userBill);
		ExcelUtil.export(response, "用户账单数据" + DateUtil.time(), "用户账单数据", listExcel, UserBillExcel.class);
	}
}
